﻿Imports System.Windows.Forms


Public Class ProvidersDialog

    Public TheUPIN As String

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim IntTest As Integer

        'Do validation
        If UserFormProviderTypeCheck(TypeBox.Text, "AdminProvidersForm") <> "W" Then
            MessageBox.Show("Currently you do not have permission to add providers of this type." & vbNewLine & _
                "Contact TheStore.EFA@education.gsi.gov.uk to change permissions." & TypeBox.Text, "Add Provider", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        If UPINBox.Text = "" Then
            MessageBox.Show("You must enter a UPIN", "Missing UPIN", MessageBoxButtons.OK, MessageBoxIcon.Error)
            UPINBox.Focus()
            Exit Sub
        End If

        If NameBox.Text = "" Then
            MessageBox.Show("You must enter a Provider Name", "Missing Provider Name", MessageBoxButtons.OK, MessageBoxIcon.Error)
            NameBox.Focus()
            Exit Sub
        End If

        If TypeBox.Text = "" Then
            MessageBox.Show("You must select a Provider Type", "No Provider Type Selected", MessageBoxButtons.OK, MessageBoxIcon.Error)
            TypeBox.Focus()
            Exit Sub
        Else
            If TypeBox.SelectedValue <> "Schoo" Then
                ParentContractBox.Text = ""
            End If
        End If

        Try
            If URNBox.Text.Length <> 0 Then
                IntTest = URNBox.Text
            End If
        Catch ex As Exception
            MessageBox.Show("URN must be an integer", "Error", MessageBoxButtons.OK)
            URNBox.Focus()
            Exit Sub
        End Try

        Try
            If EstabBox.Text.Length <> 0 Then
                IntTest = EstabBox.Text
            End If
        Catch ex As Exception
            MessageBox.Show("Establishment number must be an integer", "Error", MessageBoxButtons.OK)
            EstabBox.Focus()
            Exit Sub
        End Try

        If TheUPIN <> UPINBox.Text Then
            'Does this UPIN already exist

            Dim Thedata As DataView

            Thedata = LoadDataView("Select UPIN from Providers where UPIN = " & UPINBox.Text)

            If Thedata.Count <> 0 Then
                MessageBox.Show("This UPIN already exists.", "UPIN exists", MessageBoxButtons.OK, MessageBoxIcon.Error)
                UPINBox.Focus()
                Exit Sub
            End If
        End If

        'check to make sure that the UAI is unique
        If TypeBox.SelectedValue = "Acade" Then
            If Trim(UAIBox.Text) = "" Then
                TheTabControl.SelectedIndex = 2
                UAIBox.Focus()
                MessageBox.Show("You must enter a UAI if this provider is an Academy", "No UAI entered", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If

            If FundingMethodBox.SelectedValue = Nothing Then
                TheTabControl.SelectedIndex = 2
                FundingMethodBox.Focus()
                MessageBox.Show("You must select a funding method for this provider", "No Funding Method entered", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
        End If


        If Len(Trim(UAIBox.Text)) <> 0 Then
            Dim sql As String
            If TheUPIN = "" Then
                sql = "select UPIN from providers where UAI = '" & UAIBox.Text & "'"
            Else
                sql = "select UPIN from providers where UPIN <> " & TheUPIN & " and UAI = '" & UAIBox.Text & "'"
            End If


            If LoadDataView(sql).Count <> 0 Then
                TheTabControl.SelectedIndex = 2
                UAIBox.Focus()
                MessageBox.Show("The UAI already exists, please change it", "UAI already exists", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If

        End If


        If URNBox.Text.Length = 0 Then
            URNBox.Text = "null"
        End If

        If EstabBox.Text.Length = 0 Then
            EstabBox.Text = "null"
        End If

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub ProvidersDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If TypeBox.SelectedValue = "Acade" Then
            UAIBox.Enabled = True
        Else
            UAIBox.Enabled = False
        End If

        UPINBox.Focus()

    End Sub

    Public Sub FundingStreamBoxTick()
        Dim Sql As String

        For i = 0 To FundingStreamBox.Items.Count - 1
            FundingStreamBox.SelectedItems.Clear()
            FundingStreamBox.SelectedIndex = i

            If UPINBox.Text <> "" Then
                Sql = "select * from dbo.Provider_Funding_Streams where UPIN = " & UPINBox.Text & " " & _
                  "and Budget_ID = '" & FundingStreamBox.SelectedValue.ToString & "' and Period_ID = " & FundingPeriodBox.SelectedValue

                If LoadDataView(Sql).Count <> 0 Then
                    FundingStreamBox.SetItemChecked(i, True)
                Else
                    FundingStreamBox.SetItemChecked(i, False)
                End If
            End If

        Next

    End Sub

    Public Sub SaveFundingStreams()
        'only call me when providersdialog is open


        'first delete all fundingstreams from the table for this UPIN

        Dim sql As String
        sql = "delete from Provider_Funding_Streams where UPIN = " & UPINBox.Text & " and Period_ID = " & FundingPeriodBox.SelectedValue
        RunNonQuery(sql)

        Dim CheckedIndexes As New ArrayList
        Dim CheckedIds As New ArrayList
        Dim i As Integer
        For Each item As String In FundingStreamBox.CheckedIndices
            CheckedIndexes.Add(item)
        Next
        For i = 0 To CheckedIndexes.Count - 1
            FundingStreamBox.SelectedItems.Clear()
            FundingStreamBox.SelectedIndex = CheckedIndexes.Item(i)
            CheckedIds.Add(FundingStreamBox.SelectedValue)
        Next
        FundingStreamBox.SelectedItems.Clear()


        If CheckedIds.Count <> 0 Then

            Dim x As Integer

            For x = 0 To CheckedIds.Count - 1

                sql = "insert into Provider_Funding_Streams (Period_ID, UPIN, Budget_ID) values (" & _
                      FundingPeriodBox.SelectedValue & ", " & UPINBox.Text & ", '" & CheckedIds.Item(x).ToString & "')"
                RunNonQuery(sql)

            Next

        End If

    End Sub

    Public Sub LoadPreProvidersBox()

        Dim AView As DataView
        Dim Arec As DataRowView

        Dim str(2) As String
        Dim itm As ListViewItem

        Dim Sql As String

        Sql = "select pp.UPIN, p.Provider_Name, Pre_UPIN, p2.Provider_Name as Provider_name_pre " & _
                "from dbo.Predecessor_Providers pp, Providers P, " & _
                "(Select UPIN, Provider_Name from Providers) p2 " & _
                "where p.UPIN = pp.UPIN " & _
                "and pp.Pre_UPIN = p2.UPIN " & _
                "and pp.UPIN = " & TheUPIN


        With PreProView
            .FullRowSelect = True
            .Items.Clear()

            AView = LoadDataView(Sql)

            For Each Arec In AView
                str(0) = Arec.Item("Pre_UPIN").ToString
                str(1) = Arec.Item("Provider_name_pre").ToString

                itm = New ListViewItem(str)
                .Items.Add(itm)
            Next

        End With

    End Sub

    

    Private Sub TypeBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TypeBox.SelectedIndexChanged
        Try
            If TypeBox.SelectedValue = "Schoo" Then
                ParentContractBox.Enabled = True
                UAIBox.Enabled = False
            Else
                ParentContractBox.Enabled = False

                If TypeBox.SelectedValue = "Acade" Then
                    UAIBox.Enabled = True
                Else
                    UAIBox.Enabled = False
                End If

            End If
        Catch ex As Exception
            'not ready yet
        End Try

        LoadSubTypes()
        
    End Sub

    Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click

    End Sub

    Private Sub AddPreBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddPreBut.Click
        'Add a predecessor school
        Search_Form.WhoSentForMe = "ProvidersDialog"
        Dim PreUPIN As String
        Dim ProvName As String
        Dim sql As String
        Dim Str(2) As String
        Dim itm As ListViewItem

        If Search_Form.ShowDialog = Windows.Forms.DialogResult.OK Then
            With Search_Form

                PreUPIN = .SearchResults_DataViewGrid.Item(.SearchResults_DataViewGrid.Columns("UPIN").Index, .SearchResults_DataViewGrid.CurrentRow.Index).Value.ToString
                ProvName = .SearchResults_DataViewGrid.Item(.SearchResults_DataViewGrid.Columns("Provider").Index, .SearchResults_DataViewGrid.CurrentRow.Index).Value.ToString


                Str(0) = PreUPIN
                Str(1) = ProvName

                itm = New ListViewItem(Str)
                PreProView.Items.Add(itm)


                'sql = "insert into Predecessor_Providers Values(" & TheUPIN & "," & PreUPIN & ")"

                'RunNonQuery(sql)

            End With
            'LoadPreProvidersBox()
        End If
    End Sub

    Private Sub DelPreBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DelPreBut.Click

        If PreProView.SelectedItems.Count = 0 Then Exit Sub

        If MessageBox.Show("Would you like to remove this provider from the list?", "Remove provider?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            'sql = "delete from Predecessor_Providers where UPIN = " & TheUPIN & " and Pre_UPIN = " & PreProView.SelectedItems(0).Text
            'RunNonQuery(sql)
            'LoadPreProvidersBox()

            PreProView.SelectedItems(0).Remove()

        End If
    End Sub

    Private Sub AcademyType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AcademyType.SelectedIndexChanged
        If AcademyType.SelectedIndex = 1 Then
            ConverterDateBox.Enabled = True
        Else
            ConverterDateBox.Enabled = False
        End If
    End Sub

    Private Sub MultiSponsorCheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MultiSponsorCheck.CheckedChanged
        If MultiSponsorCheck.Checked Then
            MultiSponsorNameBox.Enabled = True
        Else
            MultiSponsorNameBox.Enabled = False
        End If
    End Sub

    Private Sub SubTypeBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubTypeBox.SelectedIndexChanged

    End Sub

    Public Sub LoadSubTypes()
        Dim SQL As String

        If TypeBox.Items.Count = 0 Then Exit Sub

        SQL = "select Provider_Subtype_Code, Provider_Subtype_Description from Provider_Subtypes where Provider_Type_Code = '" & TypeBox.SelectedValue.ToString & "'"
        LoadComboBox(SubTypeBox, SQL, "Provider_Subtype_Code", "Provider_Subtype_Description")
    End Sub

    Public Sub LoadFundingMethods()
        Dim SQL As String

        SQL = "Select * from Acad_Funding_Methods"
        LoadComboBox(FundingMethodBox, SQL, "Funding_ID", "Funding_Name")
    End Sub

    Private Sub FundingPeriodBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles FundingPeriodBox.SelectedIndexChanged
        FundingStreamBoxTick()

    End Sub

    Private Sub SaveFundingStreamsButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveFundingStreamsButton.Click
        SaveFundingStreams()

    End Sub
End Class
